﻿//编辑对话框
var dlg = new MyAppDialog(900, 600);

//列定义
var cols = [[
                { field: "Id", title: 'Id', width: 10, hidden: true },
                { field: "ck", title: '', width: 20, checkbox: true },
                { field: "Rescategory", title: '分类', sortable: true, width: 60, align: 'left' },
                { field: "Resname", title: '资源名称', sortable: true, hidden: true, width: 100, align: 'left' },
                { field: "Reskey", title: 'Key', sortable: true, width: 100, align: 'left' },
                { field: "Lang", title: '语言', sortable: true, width: 60, align: 'left' },
                { field: "Rescontent", title: '值', sortable: true, width: 160, align: 'left' },
                {
                    field: "Reskey1", title: 'View引用', sortable: true, width: 260, align: 'left', formatter: function (value, rec) {
                        return "CSMSLang.Get(\"" + rec.Reskey + "\")";
                    }
                },
                {
                    field: "Reskey2", title: 'Js引用', sortable: true, width: 260, align: 'left', formatter: function (value, rec) {
                        return "csms_lang." + rec.Reskey + "";
                    }
                },
                {
                    field: "Resjs", title: 'JS资源', sortable: true, width: 40, align: 'center', formatter: function (value, rec) {
                        if (value == 1)
                            return "<i class='iconfont icon-check1' style='color:green;'></i>";
                        else
                            return "";
                    }
                },
                { field: "Updatetime", title: '最后修改', sortable: true, width: 100, align: 'center' }
]];


$(document).ready(function () {
    //删除
    $("#btndel").click(function (evt) { evt.preventDefault(); del(); });
    //添加
    $("#btnadd").click(function (evt) {
        evt.preventDefault();
        dlg.setSize(700, 480);
        dlg.setURL("新增资源", baseurl + "Manage/Langres/Edit/0?" + Math.random());
        dlg.show();
    });
    //生成服务器端文件
    $("#btngenjs").click(function (evt) {
        evt.preventDefault();
        $.ajax({
            type: "post",
            url: baseurl + "Manage/Langres/GenJs",
            success: function (e) {
                if (e.Ret == "0") {
                    $.messager.alert(csms_lang.message_info_title, e.Msg, "info");
                } else {
                    $.messager.alert(csms_lang.message_error, e.Msg, "error");
                }
            }
        });
    });
    //下载
    $(".btndown").click(function (evt) {
        location.href = baseurl + "Manage/Langres/Down?lang=" + $(this).attr("data-lang");
    });
    //查询
    $("#btnquery").click(function (evt) {
        evt.preventDefault();
        var params = $("#datagrid").datagrid('options').queryParams;
        params.s = $("#searchkey").val();
        params.lang = $("#lang").val();
        params.rescategory = $("#rescategory").val();
        $("#datagrid").datagrid("load");
    });

    $("#searchkey").keydown(function (event) {
        if (event.keyCode == $.ui.keyCode.ENTER) {
            event.preventDefault();
            $("#btnquery").click();
            return false;
        }
    });

    $("#lang").change(function () {
        $("#btnquery").click();
    });
    $("#rescategory").change(function () {
        $("#btnquery").click();
    });

    LoadDatagrid();
});

//关闭对话框
function closeDialog(reload) {
    dlg.close();

    if (reload)
        $("#datagrid").datagrid("reload");
}

//加载列表数据
function LoadDatagrid() {
    var $dg = $("#datagrid");
    $dg.datagrid({
        title: '资源列表',
        url: baseurl + 'Manage/Langres/LangResListJson',
        fit: true,
        fitColumns: true,
        striped: true,
        nowrap: true,
        idField: 'Id',
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        pageList: [10, 20, 30, 45, 60, 75],
        rownumbers: true,
        singleSelect: false,
        queryParams: {
            s: "",
            lang: $("#lang").val()
        },
        sortName: '',
        sortOrder: 'asc',
        columns: cols,
        onLoadSuccess: function () {
            //多选时，加载完成后清除所有选中项（不然可能会有残留选中项）
            $dg.datagrid("clearSelections");
        },
        onDblClickRow: function (rowIndex, row) {
            dlg.setSize(700, 480);
            dlg.setURL("编辑资源", baseurl + "Manage/Langres/Edit/" + row.Id + "?" + Math.random());
            dlg.show();
        },
        //onSelect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //},
        //onUnselect: function (rowIndex, row) {
        //    var selectedRows = $dg.datagrid("getSelections");
        //    if (selectedRows.length == 0) {
        //        $("#btndel").attr("disabled", true);
        //    } else {
        //        $("#btndel").attr("disabled", false);
        //    }
        //}
    });
    //PaginationInit();
}

//删除
function del() {
    var row = $("#datagrid").datagrid('getSelections');
    if (row.length == 0) {
        $.messager.alert(csms_lang.message_info_title, csms_lang.message_delete_choosedelete);
        return;
    }
    $.messager.confirm(csms_lang.message_confirm, csms_lang.message_delete_confrim, function (r) {
        if (r) {
            var ids = "";
            for (var i = 0; i < row.length; i++) {
                ids += row[i].Id + ",";
            }

            $.ajax({
                type: "post",
                url: baseurl + "Manage/Langres/Delete",
                data: { ids: ids },
                success: function (e) {
                    if (e.Ret == "0") {
                        $("#datagrid").datagrid("reload");
                    } else {
                        $.messager.alert(csms_lang.message_error, e.Msg, "error");
                    }
                }
            });
        }
    })

}
